from flask import Blueprint, request

from utils.rate_limit_decorator import rate_limit
from utils.response import success, fail
from config import SPIDER_COOIKE

from services.weibo_service import weibo_info_service_by_page, weibo_info_month_ago_service, upload_weibo_info_list_service

weibo = Blueprint('weibo', __name__)

@weibo.route('/get_weibo_info_by_page')
# 根据页数获取微博数据
def get_weibo_info_by_page():
    user_id = request.args.get('user_id', default=None, type=int)
    page = request.args.get('page', default=None, type=int)
    if not user_id or not page:
        return fail('params is required')
    result = weibo_info_service_by_page(user_id, page)
    if result is None:
        return fail('weibo not found')
    return success(result)

# 根据用户id获取它近一个月的数据，展示的时候展示10条即可
@weibo.route('/get_weibo_info_recent_month', methods=['POST'])
def get_weibo_info_recent_month():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    # 返回一个字典，包含一个键值对，键为'result'，值为123
    if 'user_id' not in params or 'cookie' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    cookie = params['cookie']
    result = weibo_info_month_ago_service(user_id, cookie)
    if result is None:
        return fail('weibo not found')
    return success(result)


@weibo.route('/get_weibo_info_recent_month_no_cookie', methods=['POST'])
@rate_limit(interval=30)
def get_weibo_info_recent_month_no_cookie():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    # 返回一个字典，包含一个键值对，键为'result'，值为123
    if 'user_id' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    result = weibo_info_month_ago_service(user_id, SPIDER_COOIKE)
    if result is None:
        return fail('weibo not found')
    return success(result)

# 上传微博数据列表
# @weibo.route('/upload_weibo_info_list', methods=['POST'])
# def upload_weibo_info_list():
#     if not request.is_json:
#         return fail('request body must be json')
#     # 返回一个字典，包含一个键值对，键为'result'，值为123
#     data = request.get_json()
#     if not data['data']:
#         return fail('upload data error')
#     result = upload_weibo_info_list_service(data['data'])
#     return success(result)
    


